<template>
<!-- home界面主体框架 -->
<el-container class="total">
  <!-- home界面头部制作 -->
  <el-header>
    <!-- 网站logo的图片 -->
      <div>
        <img src="../../assets/logo.png" width="50px">
        <span>电商后台管理系统</span>
      </div>
      <el-button type="info" @click="clearToken">退出</el-button>
  </el-header>
  <el-container>
    <!-- 侧边栏导航的制作 -->
    <el-aside :width="collapse ? '64px':'200px'">
      <!-- 菜单导航的主体框架 -->
      <el-menu background-color="#333744" text-color="#fff"  
      :unique-opened ='true' :collapse="collapse" 
      :collapse-transition='false' :router="true"
      :default-active="this.activelight">
      <!-- this.$route.path也可以使用，不过只能在home不销毁的情况下使用，如果跳转到另一个页面，home销毁了
      重新创建又是被路由重定向到了welcome -->
      <!-- 导航顶部的缩放按钮 -->
      <div class="collapse" @click="mutex" >|||</div>
      <!-- 一级菜单导航 -->
        <el-submenu  :index="item.id.toString()" v-for="item in MenuList" :key="item.id">
        <template slot="title">
          <i :class="IconSubMenu[item.id]"></i>
          <span>{{item.authName}}</span>
        </template>
        <!-- 二级菜单导航 -->
          <el-menu-item :index="'/'+subitem.path.toString()"
           v-for="subitem in item.children" :key="subitem.id" 
           @click="totalclick(item,subitem)">
            <i class="el-icon-menu"></i>
            <span>{{subitem.authName}}</span>
          </el-menu-item>
      </el-submenu>
      </el-menu>
    </el-aside>
    <el-main>
      <router-view ></router-view>
    </el-main>
  </el-container>
</el-container>
    
</template>


<script>

import { netrequest } from '../../network/request'

export default {
  name: 'home',
  data(){
    return{
      MenuList:[],
      IconSubMenu :{
        '125':'el-icon-s-custom',
        '103':'el-icon-s-management',
        '101':'el-icon-s-shop',
        '102':'el-icon-s-order',
        '145':'el-icon-s-data',
      },
      collapse :false,
      activelight:''
    }
  },
  methods:{
    clearToken(){
      // 当页面跳转到这个home页面之后，给home页面设置个退出登录的按钮，当点击之后，控制路由跳转到
      // 登录页面，然后清除token以免可以直接从登录页面更改路径直接进入home页面
      this.$router.push('/login')
      window.sessionStorage.removeItem('token')
      window.sessionStorage.removeItem('activelight')

    },
    async getmenulist(){
    const {data:res} = await netrequest({method:'get',url:'/menus'})
    console.log(res);
      if(res.meta.status !== 200)  {this.$message.error(res.meta.msg);}
      this.$message.success(res.meta.msg)
      this.MenuList = res.data
      console.log(this.MenuList);
    },
    mutex(){
      this.collapse = !this.collapse
    },
    totalclick(item,subitem){
      this.gethighlight('/'+subitem.path.toString())
      this.transport(item,subitem)

    } ,
    gethighlight(active){
      window.sessionStorage.setItem('activelight',active)
      this.activelight = active
    },
    transport(item,subitem){
        this.$store.commit('transform',{item,subitem})
    } 
  },
  created(){
    this.getmenulist()
    this.activelight = window.sessionStorage.getItem('activelight')
  }
}
</script>

<style scoped>
.total{
  height: 100%;
}
.el-header{
  display: flex;
  justify-content: space-between;
  align-items: center;
  color: white;
  font-size: 20px;
  background: #373d41;
  padding-left: 0;
}
.el-header > div{
  display: flex;
  align-items: center;
}
.el-aside{
  background: #333744;
}
.el-menu{
  border: none;
}
.collapse{
  color: white;
  text-align: center;
  background: #4a5064;
  letter-spacing: .12em;
  line-height: 21px;
}

</style>

